<?php

class Admin_SupportController extends Zend_Controller_Action
{

    private $idcat = null;

    private $_model = null;

    public function init()
    {
        /* Initialize action controller here */
        $this->_model = new Application_Model_DbTable_OnlineSupport();
    }

    public function indexAction()
    {
        // action body
        
    }

    public function addAction()
    {
        
        $mdUser = new Application_Model_DbTable_User();
        $selected = $this->_request->getParam("selectuser",null);
        if($selected!=null){
            $iduser = $selected;
            $user_info = $mdUser->find($iduser)->current();
            $sdata = $user_info->toArray();
            unset($sdata['password']);
            $this->_helper->json->sendJson($sdata);
        }
        $form = new Application_Form_Support();
        $r_users = $mdUser->fetchAll();
        $ops[0] = "Chọn tài khoản...";
        foreach($r_users as $u){
            $ops[$u->idusers] = $u->username;
        }
        $form->users_id->addMultiOptions($ops);
        if($this->_request->isPost()){
            if($form->isValid($_POST)){
                $data = $form->getValues();
                $row = $this->_model->createRow($data);
                $row->save();
                
                $this->_redirect($this->view->url(array(
                    'module'=>"admin",
                    'controller' => "support",
                    'action' => "index"
                )));
            }else{
                $errors = $form->getMessages();
                foreach($errors as $key=>$value){
                    $form->$key->setAttrib("class","error");
                }
            }
        }
        $this->view->form = $form;
    }

    public function listAction()
    {
        $choice = $this->_request->getParam("choice");
        if(!empty($choice)){
            $idsup = intval($this->_request->getParam("id"));
            $rowsup = $this->_model->find($idsup)->current();
            $n_status = ($rowsup->status)?0:1;
            if($choice=="multi"){
                $result = $this->_model->update(array("status"=>$n_status),"idsupports=$idsup");
                $this->_helper->json->sendJson($result);
            }
        }
        
        $do = $this->_request->getParam("do");
        if($do=="del"){
            $id = $this->_request->getParam("id");
            $result = $this->_model->delete("idsupports=$id");
            $this->_helper->json->sendJson($result);
        }
        // action body
         $sortby = $this->_request->getParam("sortby",null);
         $sorttype = $this->_request->getParam("sorttype", "asc");
         $filter_field = $this->_request->getParam("filter_field", null);
         $filter_value = $this->_request->getParam("filter_value");
         $this->view->sort = array("by"=>$sortby, "type"=>$sorttype);
         
         $filter = null;
         if(!empty($filter_field))
         {
            $filter[$filter_field] = $filter_value;
         }
         $adapter = $this->_model->fetchPaginatorAdapter($filter,$sortby,$sorttype);
         $paginator = new Zend_Paginator($adapter);
         
         //Show 10 dòng trên 1 trang
         $paginator->setItemCountPerPage(10);
         $page = $this->_request->getParam("page",1);
         $paginator->setCurrentPageNumber($page);
         $this->view->datas = $paginator;
        $this->_helper->layout()->disableLayout();
        $this->_response->clearBody();
    }

    public function editAction()
    {
        
        $mdUser = new Application_Model_DbTable_User();
        
        $form = new Application_Form_Support();
        $r_users = $mdUser->fetchAll();
        $ops[0] = "Chọn tài khoản...";
        foreach($r_users as $u){
            $ops[$u->idusers] = $u->username;
        }
        $form->users_id->addMultiOptions($ops);
        $form->submit->setLabel("Lưu");
        $id = $this->_request->getParam("id");
        $row = $this->_model->find($id)->current();
        $form->populate($row->toArray());
        
        if($this->_request->isPost()){
            if($form->isValid($_POST)){
                $data = $form->getValues();
                
                unset($data['idsuports']);
                $this->_model->update($data, "idsupports=$id");
                
                $this->_redirect($this->view->url(array(
                    'module'=>"admin",
                    'controller' => "support",
                    'action' => "index"
                )));
            }else{
                $errors = $form->getMessages();
                foreach($errors as $key=>$value){
                    $form->$key->setAttrib("class","error");
                }
            }
        }
        $this->view->form = $form;
    }
    
    public function delAction()
    {
        $id = $this->_request->getParam("id",null);
        $result = null;
        if($id!=null)
        {
            $row = $this->_model->find($id)->current();
            $result = $row->delete();
        }
        $this->_helper->json->sendJson($result);
    }

}













